package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.estatewar.EstateWarRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.*;
import java.util.*;

public class EstateWarRecordDaoImpl extends TempDataObjectDao<EstateWarRecord> {

    public LinkedList<EstateWarRecord> getEstateWarRecordList(long userId, int maxCount) {
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<EstateWarRecord> dataList = new LinkedList<>();
        String sql = "SELECT * FROM t_u_estate_record where userId=? order by recordTime desc limit " + maxCount;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                EstateWarRecord data = parse(rs);
                dataList.addLast(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean addData(EstateWarRecord data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_estate_record(recordUid,userId,isWin,recordTime,enemyUserId,enemyBaseData,oldType,oldPos,nowType,nowPos"
                + ") VALUES(?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getRecordUid());
            call.setLong(i++, data.getUserId());
            call.setBoolean(i++, data.isWin());
            call.setLong(i++, data.getRecordTime());
            call.setLong(i++, data.getEnemyUserId());
            call.setString(i++, data.getEnemyBaseDataJsonStr());
            call.setInt(i++, data.getOldType());
            call.setInt(i++, data.getOldPos());
            call.setInt(i++, data.getNowType());
            call.setInt(i++, data.getNowPos());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(EstateWarRecord data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_estate_record set userId=?,isWin=?,recordTime=?,enemyUserId=?,enemyBaseData=?,oldType=?,oldPos=?,nowType=?,"
                + "nowPos=? where recordUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setBoolean(i++, data.isWin());
            call.setLong(i++, data.getRecordTime());
            call.setLong(i++, data.getEnemyUserId());
            call.setString(i++, data.getEnemyBaseDataJsonStr());
            call.setInt(i++, data.getOldType());
            call.setInt(i++, data.getOldPos());
            call.setInt(i++, data.getNowType());
            call.setInt(i++, data.getNowPos());
            call.setString(i++, data.getRecordUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected EstateWarRecord parseData(ResultSet rs) throws SQLException {
        EstateWarRecord data = new EstateWarRecord();
        data.setRecordUid(rs.getString("recordUid"));
        data.setUserId(rs.getLong("userId"));
        data.setWin(rs.getBoolean("isWin"));
        data.setRecordTime(rs.getLong("recordTime"));
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        String baseUserInfoStr = rs.getString("enemyBaseData");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setEnemyBaseData(userBaseInfo);
        }
        data.setOldType(rs.getInt("oldType"));
        data.setOldPos(rs.getInt("oldPos"));
        data.setNowType(rs.getInt("nowType"));
        data.setNowPos(rs.getInt("nowPos"));
        return data;
    }
}
